package com.gw.yunke.service.impl;

import com.gw.yunke.dao.InstituteMapper;
import com.gw.yunke.dao.SpecialtyMapper;
import com.gw.yunke.domain.Institute;
import com.gw.yunke.domain.InstituteExample;
import com.gw.yunke.domain.Specialty;
import com.gw.yunke.domain.SpecialtyExample;
import com.gw.yunke.domain.dto.InstitutesResp;
import com.gw.yunke.service.IInstituteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class InstituteServiceImpl implements IInstituteService{
  @Autowired
  private InstituteMapper instituteMapper;
  @Autowired
  private SpecialtyMapper specialtyMapper;

  @Override
  public List<Institute> getInstitutes() {
    InstituteExample instituteExample = new InstituteExample();
    InstituteExample.Criteria criteria = instituteExample.createCriteria();
    return instituteMapper.selectByExample(instituteExample);
  }

  @Override
  public List<Specialty> getSpecialtyByInstitute(String instituteCode) {
    SpecialtyExample specialtyExample = new SpecialtyExample();
    SpecialtyExample.Criteria criteria = specialtyExample.createCriteria();
    criteria.andInstituteCodeEqualTo(instituteCode);

    return specialtyMapper.selectByExample(specialtyExample);
  }

  @Override
  public List<Specialty> getSpecialty() {
    SpecialtyExample specialtyExample = new SpecialtyExample();
    SpecialtyExample.Criteria criteria = specialtyExample.createCriteria();
    return specialtyMapper.selectByExample(specialtyExample);
  }

  @Override
  public List<InstitutesResp> getAllInfo() {
    List<InstitutesResp> institutesRespList = new ArrayList<>();

    List<Institute> instituteList = getInstitutes();

    for(Institute institute : instituteList){
      InstitutesResp resp = new InstitutesResp();
      List<Specialty> specialtyList = getSpecialtyByInstitute(institute.getCode());

      resp.setCode(institute.getCode());
      resp.setName(institute.getName());
      resp.setSpecialtyList(specialtyList);
      institutesRespList.add(resp);
    }
    return institutesRespList;
  }

  public String getSpecialtyNameByCode(String specialtyCode){
    SpecialtyExample specialtyExample = new SpecialtyExample();
    SpecialtyExample.Criteria criteria = specialtyExample.createCriteria();
    criteria.andCodeEqualTo(specialtyCode);
    List<Specialty> specialtyList = specialtyMapper.selectByExample(specialtyExample);
    return specialtyList.get(0).getName();
  }
}
